package com.web.exiaodao.business.service.impl;

import com.web.exiaodao.business.entity.ConfigurationTable;
import com.web.exiaodao.business.repository.ConfigurationTableRepository;
import com.web.exiaodao.business.service.ConfigurationTableService;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.XaUtil;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service("ConfigurationTableService")
@Transactional(readOnly = false)
public class ConfigurationTableServiceImpl extends BaseService<ConfigurationTable> implements ConfigurationTableService {

	private static final Logger LOGGER = Logger.getLogger(ConfigurationTableServiceImpl.class);

	@Autowired
	private ConfigurationTableRepository configurationTableRepository;
	
	/**
	 * 查询单条ConfigurationTable信息
	 * //@param tId
	 * @return 返回单个ConfigurationTable对象
	 * @throws BusinessException
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public XaResult<ConfigurationTable> findOne(Long modelId) throws BusinessException {
		ConfigurationTable obj = new ConfigurationTable();
		if(modelId != 0){
			obj = configurationTableRepository.findByIdAndStatusNot(modelId,XaConstant.Status.delete);
		}
		XaResult<ConfigurationTable> xr = new XaResult<ConfigurationTable>();
		if (XaUtil.isNotEmpty(obj)) {
			xr.setObject(obj);
			LOGGER.info("后台----->查询单个实体----->ConfigurationTable");
		} else {
			xr.error(XaConstant.Message.object_not_find);
			LOGGER.info(XaConstant.Message.object_not_find);
		}
		return xr;
	}

	/**
	 * 分页查询状态非status的ConfigurationTable数据
	 * @param status
	 * @param filterParams
	 * @param pageable
	 * @return 返回对象ConfigurationTable集合
	 * @throws BusinessException
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public XaResult<Page<ConfigurationTable>> findListNEStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable) throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示非删除的所有数据
			status = XaConstant.Status.delete;
		}
		filters.put("status", new SearchFilter("status", Operator.NE, status));
		Page<ConfigurationTable> page = configurationTableRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ConfigurationTable.class), pageable);
		XaResult<Page<ConfigurationTable>> xr = new XaResult<Page<ConfigurationTable>>();
		xr.setObject(page);
		LOGGER.info("后台----->分页查询实体集合（状态不等于）----->ConfigurationTable");
		return xr;
	}

	/**
	 * 分页查询状态status的ConfigurationTable数据
	 * @param status
	 * @param filterParams
	 * @param pageable
	 * @return 返回对象ConfigurationTable集合
	 * @throws BusinessException
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public XaResult<Page<ConfigurationTable>> findListEQStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable) throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示正常数据
			status = XaConstant.Status.valid;
		}
		filters.put("status", new SearchFilter("status", Operator.EQ, status));
		Page<ConfigurationTable> page = configurationTableRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ConfigurationTable.class), pageable);
		XaResult<Page<ConfigurationTable>> xr = new XaResult<Page<ConfigurationTable>>();
		xr.setObject(page);
		LOGGER.info("后台----->分页查询实体集合（状态等于）----->ConfigurationTable");
		return xr;
	}

	/**
	 * 保存ConfigurationTable信息
	 * @param model
	 * @return
	 * @throws BusinessException
	 */
	@Transactional(rollbackFor = Exception.class)
	public XaResult<ConfigurationTable> saveOrUpdate(ConfigurationTable model) throws BusinessException {
		ConfigurationTable obj = null;
		if(XaUtil.isNotEmpty(model.getId())){
			obj = configurationTableRepository.findOne(model.getId());
		}else{
			obj = new ConfigurationTable();
		}
		obj.setCreateUser(XaUtil.isNotEmpty(model.getCreateUser()) ? model.getCreateUser() : obj.getCreateUser());
		obj.setSignin_lottery_coin_per(XaUtil.isNotEmpty(model.getSignin_lottery_coin_per()) ? model.getSignin_lottery_coin_per() : obj.getSignin_lottery_coin_per());
		obj.setSignin_winningRate(XaUtil.isNotEmpty(model.getSignin_winningRate()) ? model.getSignin_winningRate() : obj.getSignin_winningRate());
		obj.setSignin_inance_expr_coin(XaUtil.isNotEmpty(model.getSignin_inance_expr_coin()) ? model.getSignin_inance_expr_coin() : obj.getSignin_inance_expr_coin());
		obj.setCmp_intro(XaUtil.isNotEmpty(model.getCmp_intro()) ? model.getCmp_intro() : obj.getCmp_intro());
		obj.setCmp_cellPhone(XaUtil.isNotEmpty(model.getCmp_cellPhone()) ? model.getCmp_cellPhone() : obj.getCmp_cellPhone());
		obj.setCmp_address(XaUtil.isNotEmpty(model.getCmp_address()) ? model.getCmp_address() : obj.getCmp_address());
		obj.setIncome_expenses_signin_coin(XaUtil.isNotEmpty(model.getIncome_expenses_signin_coin()) ? model.getIncome_expenses_signin_coin() : obj.getIncome_expenses_signin_coin());
		obj.setIncome_expenses_task_coin(XaUtil.isNotEmpty(model.getIncome_expenses_task_coin()) ? model.getIncome_expenses_task_coin() : obj.getIncome_expenses_task_coin());
		obj.setIncome_expenses_commet_coin(XaUtil.isNotEmpty(model.getIncome_expenses_commet_coin()) ? model.getIncome_expenses_commet_coin() : obj.getIncome_expenses_commet_coin());
		obj.setIncome_expenses_share_coin(XaUtil.isNotEmpty(model.getIncome_expenses_share_coin()) ? model.getIncome_expenses_share_coin() : obj.getIncome_expenses_share_coin());
		obj.setIncome_expenses_LeaveMessage_coin(XaUtil.isNotEmpty(model.getIncome_expenses_LeaveMessage_coin()) ? model.getIncome_expenses_LeaveMessage_coin() : obj.getIncome_expenses_LeaveMessage_coin());
		obj.setIncome_expenses_nurseExperience_coin(XaUtil.isNotEmpty(model.getIncome_expenses_nurseExperience_coin()) ? model.getIncome_expenses_nurseExperience_coin() : obj.getIncome_expenses_nurseExperience_coin());
		obj.setIncome_expenses_lottery_task_coin(XaUtil.isNotEmpty(model.getIncome_expenses_lottery_task_coin()) ? model.getIncome_expenses_lottery_task_coin() : obj.getIncome_expenses_lottery_task_coin());
		obj.setIncome_expenses_watch_viedo_coin(XaUtil.isNotEmpty(model.getIncome_expenses_watch_viedo_coin()) ? model.getIncome_expenses_watch_viedo_coin() : obj.getIncome_expenses_watch_viedo_coin());
		obj.setServiceTel(XaUtil.isNotEmpty(model.getServiceTel()) ? model.getServiceTel() : obj.getServiceTel());
		obj.setIncome_expenses_signin_asc_coin(XaUtil.isNotEmpty(model.getIncome_expenses_signin_asc_coin()) ? model.getIncome_expenses_signin_asc_coin() : obj.getIncome_expenses_signin_asc_coin());
		obj.setIncome_register_coin(XaUtil.isNotEmpty(model.getIncome_register_coin()) ? model.getIncome_register_coin() : obj.getIncome_register_coin());
		obj.setIncome_recommend_coin(XaUtil.isNotEmpty(model.getIncome_recommend_coin()) ? model.getIncome_recommend_coin() : obj.getIncome_recommend_coin());
		obj.setDistribution1Rate (XaUtil.isNotEmpty(model.getDistribution1Rate ()) ? model.getDistribution1Rate () : obj.getDistribution1Rate ());
	    obj.setDistribution2Rate (XaUtil.isNotEmpty(model.getDistribution2Rate ()) ? model.getDistribution2Rate () : obj.getDistribution2Rate ());
	    obj.setVipPrice (XaUtil.isNotEmpty(model.getVipPrice ()) ? model.getVipPrice () : obj.getVipPrice ());
		obj = configurationTableRepository.save(obj);
		XaResult<ConfigurationTable> xr = new XaResult<ConfigurationTable>();
		xr.setObject(obj);
		LOGGER.info("后台新增或编辑数据成功！");
		return xr;
	}

	/**
	 * 修改ConfigurationTable状态，可一次修改多条   3删除  -1锁定  1正常
	 * @param modelIds
	 * @param status
	 * @return 返回ConfigurationTable对象
	 * @throws BusinessException
	 */
	@Transactional(rollbackFor = Exception.class)
	public XaResult<ConfigurationTable> multiOperate(
			String modelIds,Integer status) throws BusinessException {
		XaResult<ConfigurationTable> xr = new XaResult<ConfigurationTable>();
		if(status == null){
			status = XaConstant.Status.delete;
		}
		if(modelIds != null){
			String[] ids = modelIds.split(",");
			for(String id : ids){
				ConfigurationTable obj = configurationTableRepository.findByIdAndStatusNot(Long.parseLong(id),status);
				if (XaUtil.isNotEmpty(obj)) {
					obj.setStatus(status);
					obj = configurationTableRepository.save(obj);
				}
			}
		}
		LOGGER.info("批量修改数据状态成功！");
		return xr;
	}
	
}
